﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RobotEscape.Entities;
using System.Collections.Generic;
using System.Linq;

namespace RobotEscape.Screens
{
    public class GameScreen : ScreenBase
    {
        public int levelNum = 1;
        public Level CurrentLevel;
        public Hud CurrentHud;
        public TimeSpan timeSpent;
        PauseScreen pauseScreen;
        public bool alreadyInit = false;
        double winTime = 0;
        bool playedLaserSound = false;

        public GameScreen(int level)
        {
            levelNum = level;
        }

        public override void Init()
        {
            if (!alreadyInit)
            {
                alreadyInit = true;
                pauseScreen = new PauseScreen(this);
                this.CurrentLevel = new Level(this, levelNum);
                timeSpent = TimeSpan.Zero;
                this.CurrentHud = new Hud(this);

                foreach (Exit exit in this.entities.OfType<Exit>())
                {
                    foreach (Plate plate in this.entities.OfType<Plate>())
                    {
                        switch (plate.PlateColor)
                        {
                            case PlateColor.Blue:
                                exit.bClosed = true;
                                break;
                            case PlateColor.Green:
                                exit.gClosed = true;
                                break;
                            case PlateColor.Red:
                                exit.rClosed = true;
                                break;
                            case PlateColor.Yellow:
                                exit.yClosed = true;
                                break;
                            default:
                                break;
                        }
                    }
                }

                base.Init();
            }
        }

        public override void Update(GameTime time)
        {
            if (!paused)
            {
                timeSpent += time.ElapsedRealTime;

                if (KeyState.GetKeyState(Key.Escape) && !KeyState.GetOldKeyState(Key.Escape))
                {
                    Game.Instance.SwitchScreen(pauseScreen, false);
                }

                if (KeyState.GetNewlyPressedKey(Key.Enter) || KeyState.GetNewlyPressedKey(Key.Space))
                {
                    foreach (Player player in entities.OfType<Player>())
                    {
                        if (!player.Activated)
                        {
                            player.Activated = true;
                            CurrentLevel.UnmaskSection(player.PlayerNum);
                            break;
                        }
                    }
                }

                if (KeyState.GetNewlyPressedKey(Key.PageUp) && levelNum < Game.Instance.LEVEL_COUNT)
                    Game.Instance.SwitchScreen(new GameScreen(levelNum + 1));
                if (KeyState.GetNewlyPressedKey(Key.PageDown) && levelNum > 1)
                    Game.Instance.SwitchScreen(new GameScreen(levelNum - 1));
            }

            if (!Game.Instance.hasSeenHelp)
            {
                Game.Instance.hasSeenHelp = true;
                Game.Instance.SwitchScreen(new HelpScreen(this), false);
            }

            CurrentLevel.Update(time);
            CurrentHud.Update(time);

            CheckWin(time);
            base.Update(time);
        }

        private void CheckWin(GameTime time)
        {
            bool allActivated = true;
            foreach (Plate plate in this.entities.OfType<Plate>())
            {
                if (!plate.Activated) allActivated = false;
            }

            if (allActivated)
            {
                if (winTime == 0)
                    winTime = time.TotalRealTime.TotalMilliseconds;
                else if (time.TotalRealTime.TotalMilliseconds - winTime >= 100)
                {
                    if (!playedLaserSound)
                    {
                        playedLaserSound = true;
                        Sound.Play(Sounds.Laser);
                    }
                }
            }
            else
            {
                winTime = 0;
            }
        }

        public override void Remove()
        {
            CurrentLevel.Remove();
            CurrentHud.Remove();
            base.Remove();
        }
    }
}
